﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Browser;
using GoStats.Resources;
using System.Net.Http;
using System.Net;
using System.IO;
using System.Text;
using GoStats.Model;
using System.Threading.Tasks;

namespace GoStats.ViewModels
{
    
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            this.Items = new ObservableCollection<SiteViewModel>();
        }

        public ObservableCollection<SiteViewModel> Items { get; private set; }
        Persist persist = new Persist();


        private Boolean isAvailable;

        public Boolean IsAvailable
        {
            get { return isAvailable; }
            set
            {
                isAvailable = value;
                NotifyPropertyChanged("IsAvailable");
                NotifyPropertyChanged("Items");
            }
        }

        private Boolean isLoading;

        public Boolean IsLoading
        {
            get { return isLoading; }
            set
            {
                isLoading = value;
                NotifyPropertyChanged("IsLoading");
            }
        }

        private Boolean isFailed;

        public Boolean IsFailed
        {
            get { return isFailed; }
            set
            {
                isFailed = value;
                NotifyPropertyChanged("IsFailed");
            }
        }

        private Boolean isLoginPage;

        public Boolean IsLoginPage
        {
            get { return isLoginPage; }
            set { 
                isLoginPage = value;
                NotifyPropertyChanged("IsLoginPage");
                NotifyPropertyChanged("IsNotLoginPage");
            }
        }
        public Boolean IsNotLoginPage
        {
            get { return !isLoginPage; }
        }

        public async void Load()
        {
            // When app loaded, check if there is any saved login details,
            // if exists, try to login, otherwise keep the login page.
            if (persist.Load())
            {
                IsLoginPage = true;
                UsernameBox = persist.Username;
                PasswordBox = persist.Password;
                TestLogin();
            }
            else
            {
                IsLoginPage = true;
            }

        }

        public async Task<Boolean> LoadData()
        {
                // Try to loa websites list.
                IsLoading = true;
                IsAvailable = false;
                IsFailed = false;
                List<Site> sites = await API.getSites();
                if (sites != null)
                {
                    foreach (Site s in sites)
                    {
                        this.Items.Add(new SiteViewModel()
                        {
                            SiteDetails = s,
                        });
                    }
                    IsLoading = false;
                    IsAvailable = true;
                    IsFailed = false;
                    return true;
                }
                else
                {
                    IsLoading = false;
                    IsAvailable = false;
                    IsFailed = true;
                    return true;
                }
        }

        private String usernameBox = "";
        private String passwordBox = "";

        public String UsernameBox
        {
            get { return usernameBox; }
            set { usernameBox = value; NotifyPropertyChanged("UsernameBox"); }
        }

        public String PasswordBox
        {
            get { return passwordBox; }
            set { passwordBox = value; NotifyPropertyChanged("PasswordBox"); }
        }

        public async void TestLogin(){
            // Try to login using the username/password from the text boxes.
            // If successful, call LoadData to display sites.
            IsLoading = true;
            IsAvailable = false;
            IsFailed = false;

            Boolean loggedin = await API.login(UsernameBox, PasswordBox);
            if (loggedin)
            {
                IsLoginPage = false;
                persist.Password = PasswordBox;
                persist.Username = UsernameBox;
                persist.PersistLogin();
                await LoadData();
            }
            else
            {
                IsLoading = false;
                IsAvailable = false;
                IsFailed = true;
            }
        }




        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public async void Refresh()
        {
            if(IsAvailable)
                await LoadData();
        }

        public void Logout()
        {
            if (IsAvailable)
            {
                API.Clear();
                IsLoginPage = true;
                UsernameBox = persist.Username;
                PasswordBox = persist.Password;
                IsAvailable = false;
            }
        }
    }
}